<?php
/*
 * 远程函数
 * @author     Mark <zhaody901@126.com>
 * @copyright  Copyright (c) 2013 lemon team
 * @version    $Id: 86fc6ca053858abd007c12145090d4602e5b9832 $
 * @time       14-2-7 上午10:29
 */
defined('IN_IXDCW') or exit('Access Denied');
/**
 * 将远程文件保存到指定目录
 */
class remote {
	/** @var 远程文件名称 */
    var $file;

	/** @var string 保存名称 */
    var $savename;

	/** @var 保存路径 */
    var $savepath;

	/** @var 保存到的完整的路径,包含路径和名称 */
	var $saveto;

	/** @var bool 是否覆盖 */
    var $overwrite = false;

	/** @var int 允许的最大值 */
    var $maxsize;

	/** @var string 扩展名 */
    var $ext;

	/** @var string 错误信息 */
    var $errmsg = errmsg;

	/** @var int 用户id */
	var $userid;

	/** @var 图像 */
	var $image;

	/** @var int上传时间 */
	var $uptime = 0;

	/** @var bool 是否添加用户id */
	var $adduserid = true;

	/**
	 * constructor
	 * @param        $file
	 * @param        $savepath
	 * @param string $savename
	 */
	function __construct($file, $savepath, $savename = '') {
		global $DT, $_userid;
		$this->file = $file;
		$this->userid = $_userid;
		$this->ext = File::ext($file);
		in_array($this->ext, array('jpg', 'jpeg', 'gif', 'png', 'bmp')) or $this->ext = 'jpg';
		$this->maxsize = $DT['uploadsize'] ? $DT['uploadsize']*1024 : 2048*1024;
		$this->savepath = $savepath;
		$this->savename = $savename;
    }

	/**
	 * 保存图片
	 * @return bool
	 */
	function save() {
		include load('include.lang');
        if(!$this->is_allow()) return $this->_($L['upload_not_allow']);
        $this->set_savepath($this->savepath);
        $this->set_savename($this->savename);
        if(!File::isWritable(DT_ROOT.'/'.$this->savepath)) return $this->_($L['upload_unwritable']);
		if(@copy($this->file, DT_ROOT.'/'.$this->saveto)) {
			if(!@getimagesize(DT_ROOT.'/'.$this->saveto)) {
				File::del(DT_ROOT.'/'.$this->saveto);
				return $this->_($L['upload_bad']);
			}
			if($this->maxsize > 0 && filesize(DT_ROOT.'/'.$this->saveto) > $this->maxsize) {
				File::del(DT_ROOT.'/'.$this->saveto);
				return $this->_($L['upload_size_limit'].' ('.intval($this->maxsize/1024).'Kb)');
			}
			$this->image = 1;
			return true;
		} else {
			return $this->_($L['upload_failed']);
		}
	}

	/**
	 * @return bool 路径是否符合规范
	 */
	function is_allow() {
		if(strlen($this->file) < 18 || strpos($this->file, '://') === false) return false;
		return true;
    }

	/**
	 * 设置保存路径
	 * @param $savepath
	 */
	function set_savepath($savepath) {
		$savepath = str_replace("\\", "/", $savepath);
	    $savepath = substr($savepath, -1) == "/" ? $savepath : $savepath."/";
        $this->savepath = $savepath;
    }

	/**
	 * 设置保存名称, 如果名称存在则自行重命名
	 * @param $savename
	 */
	function set_savename($savename) {
		global $DT_TIME;
        if($savename) {
            $this->savename = $this->adduserid ? str_replace('.'.$this->ext, $this->userid.'.'.$this->ext, $savename) : $savename;
        } else {
			$this->uptime = $DT_TIME;
            // 保存名称
	        $name = date('H-i-s', $this->uptime).'-'.rand(10, 99);
            $this->savename = $this->adduserid ? $name.'-'.$this->userid.'.'.$this->ext : $name.'.'.$this->ext;
        }

		$this->saveto = $this->savepath.$this->savename;		
        if(!$this->overwrite && is_file(DT_ROOT.'/'.$this->saveto)) {
			$i = 1;
			while($i) {
				$saveto = str_replace('.'.$this->ext, '('.$i.').'.$this->ext, $this->saveto);
				if(is_file(DT_ROOT.'/'.$saveto)) {
					$i++;
					continue; 
				} else {
					$this->saveto = $saveto; 
					break;
				}
			}
        }
    }

	/**
	 * @param $e
	 * @return bool
	 */
	function _($e) {
		$this->errmsg = $e;
		return false;
	}
}
?>